package IOBaseTest;

import java.io.*;
import java.io.FileReader;

/**
 * Created by yexuan on 2018/3/12.
 */
public class FileCopyDemo {
    public static void main(String[] args) {
        copy_method2();

    }

    public static void copy_method2(){
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new FileReader("buff.txt"));
            bw = new BufferedWriter(new FileWriter("copybuff.txt"));

            String line = null;
            while ((line = br.readLine())!=null){
                bw.write(line);
                bw.newLine();
                bw.flush();
            }

        }catch (IOException e){
            System.out.println(e.toString());
        }finally {
            if (br!=null)
                try {
                    br.close();
                }catch (IOException e){
                    System.out.println(e.toString());
                }
            if (bw!=null)
                try {
                    bw.close();
                }catch (IOException e){
                    System.out.println(e.toString());
                }
        }
    }

    public static void copy_method1(){
        FileReader fr = null;
        FileWriter fw = null;
        try {
            fr = new FileReader("demo.txt");
            fw = new FileWriter("copy.txt");

            char[] c = new char[1024];
            int len = 0;
            while ((len = fr.read(c))!=-1){
                fw.write(c,0,len);
            }
            fw.flush();

        }catch (IOException e){
            System.out.println(e.toString());
        }finally {
            if (fr!=null)
                try {
                    fr.close();
                }catch (IOException e){
                    System.out.println(e.toString());
                }
            if (fw!=null)
                try {
                    fw.close();
                }catch (IOException e){
                    System.out.println(e.toString());
                }
        }
    }
}
